'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with self work for additional information
 * regarding copyright ownership.  The ASF licenses self file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use self file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http:#www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.rdf.Alt import Alt
from base.rdf.AltHasNoDefaultException import AltHasNoDefaultException
from base.rdf.impl.ContainerImpl import ContainerImpl
from base.vocabulary.RDF import RDF

# import org.apache.jena.enhanced.*;
# import org.apache.jena.graph.*;
# import org.apache.jena.rdf.model.*;
# import org.apache.jena.vocabulary.RDF;

'''
 * An implementation of Alt.
'''


class AltImpl(ContainerImpl, Alt):
    # @SuppressWarnings("hiding")
    # final static public Implementation factory = Implementation():
    #     # @Override
    # # param Node n, EnhGraph eg
    # # result boolean
    # def canWrap(self, n, eg):
    #         return True
    #
    #
    #     # @Override
    # # param Node n, EnhGraph eg
    # # result EnhNode
    # def wrap(self, n, eg):
    #         return AltImpl(n, eg)

    '''
     * Creates AltMem
    '''

    # param ModelCom model
    def __init__(self, model):
        super().__init__(model)

    # TODO 以下3个方法后续需要修正
    # param String uri, ModelCom model
    def initWithUriAndModelCom(self, uri, model):
        super().initWithUriAndModelCom(uri, model)

    # param Resource r, ModelCom m
    def initWithResourceAndModelCom(self, r, m):
        super().initWithResourceAndModelCom(r, m)

    # param Node n, EnhGraph g
    def initWithNodeAndEnhGraph(self, n, g):
        super().initWithNodeAndEnhGraph(n, g)

    '''
     * get the default statement, explode if there isn't one
    '''

    # param 
    # result Statement
    def needDefaultStatement(self):
        # Statement
        stmt = self.getDefaultStatement()
        if (stmt is None):
            raise AltHasNoDefaultException(self)
        return stmt

    # @Override
    # param 
    # result RDFNode
    def getDefault(self):
        return self.needDefaultStatement().getObject()

    # @Override
    # param 
    # result Resource
    def getDefaultResource(self):
        return self.needDefaultStatement().getResource()

    # @Override
    # param 
    # result Literal
    def getDefaultLiteral(self):
        return self.needDefaultStatement().getLiteral()

    # @Override
    # param 
    # result boolean
    def getDefaultBoolean(self):
        return self.needDefaultStatement().getBoolean()

    # @Override
    # param 
    # result byte
    def getDefaultByte(self):
        return self.needDefaultStatement().getByte()

    # @Override
    # param 
    # result short
    def getDefaultShort(self):
        return self.needDefaultStatement().getShort()

    # @Override
    # param 
    # result int
    def getDefaultInt(self):
        return self.needDefaultStatement().getInt()

    # @Override
    # param 
    # result long
    def getDefaultLong(self):
        return self.needDefaultStatement().getLong()

    # @Override
    # param 
    # result char
    def getDefaultChar(self):
        return self.needDefaultStatement().getChar()

    # @Override
    # param 
    # result float
    def getDefaultFloat(self):
        return self.needDefaultStatement().getFloat()

    # @Override
    # param 
    # result double
    def getDefaultDouble(self):
        return self.needDefaultStatement().getDouble()

    # @Override
    # param 
    # result String
    def getDefaultString(self):
        return self.needDefaultStatement().getString()

    # @Override
    # param 
    # result String
    def getDefaultLanguage(self):
        return self.needDefaultStatement().getLanguage()

    # @Override
    # param 
    # result Alt
    def getDefaultAlt(self):
        return self.needDefaultStatement().getAlt()

    # @Override
    # param 
    # result Bag
    def getDefaultBag(self):
        return self.needDefaultStatement().getBag()

    # @Override
    # param 
    # result Seq
    def getDefaultSeq(self):
        return self.needDefaultStatement().getSeq()

    # @Override
    # param RDFNode o
    # result Alt
    def setDefault(self, o):
        # Statement
        stmt = self.getDefaultStatement()
        if (stmt is not None):
            self.getModel().remove(stmt)
        self.getModel().add(self, RDF.li(1), o)
        return self

    # @Override
    # param boolean o
    # result Alt
    # def setDefault(self, o):
    #     return setDefault(getModel().createTypedLiteral(o))

    # @Override
    # param long o
    # result Alt
    # def setDefault(self, o):
    #     return setDefault(getModel().createTypedLiteral(o))

    # @Override
    # param char o
    # result Alt
    # def setDefault(self, o):
    #     return setDefault(getModel().createTypedLiteral(o))

    # @Override
    # param float o
    # result Alt
    # def setDefault(self, o):
    #     return setDefault(getModel().createTypedLiteral(o))

    # @Override
    # param double o
    # result Alt
    # def setDefault(self, o):
    #     return setDefault(getModel().createTypedLiteral(o))

    # @Override
    # param Object o
    # result Alt
    # def setDefault(self, o):
    #     return setDefault(getModel().createTypedLiteral(o))

    # @Override
    # param String o
    # result Alt
    # def setDefault(self, o):
    #     return setDefault(o, "")

    # @Override
    # param String o, String l
    # result Alt
    # def setDefault(self, o, l):
    #     return setDefault(new LiteralImpl(NodeFactory.createLiteral(o, l), getModelCom()))

    # param 
    # result Statement
    def getDefaultStatement(self):
        # StmtIterator
        ite = self.getModel().listStatements(self, RDF.li(1), None)
        try:
            return ite.nextStatement() if ite.hasNext() else None
        finally:
            ite.close()
